package com.example.Sao.MyActivity;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.example.Sao.Data.BeanData;
import com.example.Sao.Data.MListAdapter;
import com.example.Sao.R;
import com.example.Sao.Serivce.SerialPortService;

public class SymbologiesSig extends MyBaseActivity {
	
	private final String Tag = "SymbologiesSig";
	
	MyApplication mApp;
    ListView  mListView;
    List<BeanData> mListData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.symbologies_sig);
		
		mApp = (MyApplication)getApplication();
		
		mListData = mApp.initSigData();
		
	    mListView = (ListView)findViewById(R.id.mList);
	        
        final MListAdapter mMListAdapter = new MListAdapter(this, mListData);
        mListView.setAdapter(mMListAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				//Log.e(Tag, "onItemClick: position = " + position);
				BeanData mBeanData = (BeanData)mMListAdapter.getItem(position);
				
				if(mBeanData.isOpened()){
					mBeanData.setOpened(false);
					mListData.get(position).setOpened(false);
					
				}else{
					mBeanData.setOpened(true);
					mListData.get(position).setOpened(true);
				}
				
		  		SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_SIG, 0);
		  		SharedPreferences.Editor editor = mSP.edit();
		  	    editor.putBoolean(mListData.get(position).getmSymbName(), mListData.get(position).isOpened() ? true : false);
		  		editor.commit();
				
				String command  = mListData.get(position).getmSubCommand();

				if(!mListData.get(position).isOpened()){
					command = command.substring(0, command.length()-2) + "0.";
				}
				
				//command = command.substring(0, command.length()-2) + "?.";

				
				Log.e(Tag, "command = " + command);
				
				Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
				intent.putExtra(SerialPortService.BARCODEPORT_WRITEDATA_EXTRA_DATA, command);
				sendBroadcast(intent);
				
				if(mListData.get(position).isOpened()){
					Toast.makeText(SymbologiesSig.this,"open " + "  " + mListData.get(position).getmSymbName(), Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(SymbologiesSig.this,"close" + "  " + mListData.get(position).getmSymbName(), Toast.LENGTH_SHORT).show();
				}

				mMListAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MyApplication.onSettings = true;
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MyApplication.onSettings = false;
	}
	

	
}
